package com.ebpm.designer.line
{
	import com.ebpm.designer.WF;
	import com.ebpm.designer.WP;
	import com.ebpm.designer.dir.LineMiddleDir;
	import com.ebpm.designer.item.Item;
	import com.ebpm.designer.mainLayout.EbpmText;
	import com.ebpm.designer.mainLayout.variable.Variable;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayList;
	import mx.containers.Canvas;
	import mx.controls.Label;
	import mx.controls.TextArea;
	import mx.formatters.DateFormatter;
	
	/**
	 *@注意：【线】的构成是通过点组成! 两点组成一条线，所以要定义线，等于定义多个【点】 
	 **/
	public class Line extends Canvas
	{
		public var variable:Variable;																														//流程参数信息
		public var lineHeadType:String;																													//线头类型【上】【下】【左】【右】
		public var startPoint:Point;																															//开始点
		public var endPoint:Point;																															//结束点
		public var points:ArrayList = new ArrayList();																						//【点】集合
		
		public var sourceItem:Item;																														//【源NodeContainer】的item
		public var targetItem:Item;																														//【目标NodeContainer】的item
		
		public var isAddDir:Boolean = false;																										//是否添加resize框：点击线的时候，会出现resize框，可以移动线
		public var isAlreadyResize:Boolean = true;																							//是否已经被Resize过，如果Resize过，坐标 Y不能改变
		public var isFirstMove:Boolean = false;																									//是不是第一次触发移动事件
		public var middleDir:LineMiddleDir;																										//【中间resize框】
		public var resizeFromDir:LineMiddleDir;																								//【源resize框】
		public var resizeToDir:LineMiddleDir;																										//【目标resize框】
		//【resizeFromExtraX resizeFromExtraY resizeToExtraX resizeToExtraY】是为了【线】在Contaier位移
		public var resizeFromExtraX:Number = 0;																								//【源节点x】位移
		public var resizeFromExtraY:Number = 0;																								//【源节点y】位移
		public var resizeToExtraX:Number = 0;																									//【目标节点x】位移
		public var resizeToExtraY:Number = 0;																									//【目标节点y】位移
		public var resizeMiddleExtra:Number = 0;																								//【中间】位移
		public var resizeStartExtra:Number = 0;																									//移动最初值
		
		public var isClick:Boolean = false;																												//是否点击了线
		public var txtStartMoveX:Number;																											//【文本框】初始x：为了移动文本框用的
		public var txtStartMoveY:Number;																											//【文本框】初始y
			
		public var txtInput:EbpmText = new EbpmText();
		public var txtLabel:Label = new Label();
		public var isInputEditing:Boolean = false;																								//是否打开【input框】正在编辑中...
		public var distanceWithEndPointWidth:Number=0;
		public var distanceWithEndPointHeight:Number=0;
		
		public function Line()
		{
			var myDateFormatter:DateFormatter=new DateFormatter();
			myDateFormatter.formatString="YYMMDDHHNNSS";
			var dateId:String = myDateFormatter.format(new Date());
			
			this.variable = new Variable();
			this.variable.name = "Line" + dateId + WF.getId();
			WF.wp.addElement(this);
			initLabelAndInput();
			initListeners();
		}
		
		private function initLabelAndInput():void {
			this.txtLabel.x = 0;
			this.txtLabel.y = 0;
			this.txtLabel.width = 0;
			this.txtLabel.height = 0;
			this.txtInput.x = 0;
			this.txtInput.y = 0;
			this.txtInput.width = 0;
			this.txtInput.height = 0;
			this.addElement(this.txtInput);
			this.addElement(this.txtLabel);
		}
		
		private function initListeners():void {
			this.addEventListener(MouseEvent.CLICK, _click);
			this.doubleClickEnabled = true;
			this.addEventListener(MouseEvent.DOUBLE_CLICK, _lableDoubleClick);
			
			txtLabel.addEventListener(MouseEvent.MOUSE_DOWN, _labelMouseDown);
			txtLabel.addEventListener(MouseEvent.MOUSE_UP, _labelMouseUp);
			txtInput.addEventListener(KeyboardEvent.KEY_UP, _inputKeyUp);
		}
		
		
		public function showLabel():void {
			txtLabel.width = this.getLabelDescWidth();
			txtLabel.height = WF.LINE_TXT_H;
			var p1:Point = this.points.getItemAt(1) as Point;
			var end:Point = this.points.getItemAt(this.points.length - 1) as Point;
			var p:Point = this.points.getItemAt(this.points.length - 2) as Point;
			if(this.lineHeadType == "left") {
				txtInput.x = end.x + WF.LINE_HEAD_W;
				txtInput.y = end.y - WF.LINE_HEAD_H - txtInput.height;
			} else if(this.lineHeadType == "right") {
				txtInput.x = end.x - WF.LINE_HEAD_W - WF.LINE_TXT_W;
				txtInput.y = end.y - WF.LINE_HEAD_H - txtInput.height;
			} else if(this.lineHeadType == "up") {
				txtInput.x = end.x - (WF.LINE_TXT_W / 2);
				txtInput.y = end.y + WF.LINE_HEAD_W;
			} else {
				txtInput.x = end.x - (WF.LINE_TXT_W / 2);
				txtInput.y = end.y - WF.LINE_TXT_H - WF.LINE_HEAD_W;
			}
			this.distanceWithEndPointWidth = this.txtLabel.x - end.x;
			this.distanceWithEndPointHeight = this.txtLabel.y - end.y;
		}
		
		public function showLabelByParserFile():void {
			txtLabel.width = getLabelDescWidth();
			txtLabel.height = WF.LINE_TXT_H;
			if(this.distanceWithEndPointWidth != 0)																																//【文本框】如果移动过 - 根据【EndPoint】计算label的x、y值
			{
				txtLabel.x = endPoint.x + this.distanceWithEndPointWidth;
				txtLabel.y = endPoint.y + this.distanceWithEndPointHeight;
			} 
			else 																																																	//【文本框】如果没有移动过
			{
				if(this.lineHeadType == "left") {
					txtLabel.x = endPoint.x + WF.LINE_HEAD_W;
					txtLabel.y = endPoint.y - WF.LINE_HEAD_H - txtInput.height;
				} else if(this.lineHeadType == "right") {
					txtLabel.x = endPoint.x - WF.LINE_HEAD_W - WF.LINE_TXT_W;
					txtLabel.y = endPoint.y - WF.LINE_HEAD_H - txtInput.height;
				} else if(this.lineHeadType == "up") {
					txtLabel.x = endPoint.x - (WF.LINE_TXT_W / 2);
					txtLabel.y = endPoint.y + WF.LINE_HEAD_W;
				} else {
					txtLabel.x = endPoint.x - (WF.LINE_TXT_W / 2);
					txtLabel.y = endPoint.y - WF.LINE_TXT_H - WF.LINE_HEAD_W;
				}
			}
		}
		
		public function _labelMouseDown(event:MouseEvent):void {
			this.txtStartMoveX = this.txtLabel.x;
			this.txtStartMoveY = this.txtLabel.y;
			WP.isMovingLineTxtEvent = true;
			WP.isAllowSelectNodeContainers = false;
			WF.wp.selectLine = this;
		}
		
		public function _labelMouseUp(event:MouseEvent):void {
			WF.wp.selectLine = null;
			var end:Point = this.points.getItemAt(this.points.length - 1) as Point;
			this.distanceWithEndPointWidth = this.txtLabel.x - end.x;
			this.distanceWithEndPointHeight = this.txtLabel.y - end.y;
		}
		
		public function movingLabel(event:MouseEvent):void {
			this.txtLabel.x = this.txtStartMoveX + WP.mouseChangeX;
			this.txtLabel.y = this.txtStartMoveY + WP.mouseChangeY;
		}
		
		public function _click(event:MouseEvent):void {
			isClick = true;
			WF.wp.selectLine = this;
			this.drawLine();
			this.addDir();
			WF.wp.hideVariablePanels();
			WF.wp.transferVariablePanel.showMe();
			WF.wp.transferVariablePanel.vc.initContent(this.variable);
		}
		
		public function _inputKeyUp(event:KeyboardEvent):void {
			
			//Enter键
			if(event.keyCode == 13) {
				hidenInput();
			}
			
		}
		
		public function _lableDoubleClick(event:MouseEvent):void {
			if(!this.isInputEditing) {
				WP.isMovingNodeContainer = false;
				this.isInputEditing = true;
				if(this.txtLabel.width != 0) {
					this.txtInput.x = this.txtLabel.x;					
					this.txtInput.y = this.txtLabel.y;
					this.txtInput.width = this.txtLabel.width + 10;
					this.txtInput.height = this.txtLabel.height;
					this.txtInput.text = this.txtLabel.text;
				} else {																																//第一次双击
					txtInput.width = WF.LINE_TXT_W;
					txtInput.height = WF.LINE_TXT_H;
					//end为箭头的方向
					var end:Point = this.points.getItemAt(this.points.length - 1) as Point;
					if(this.lineHeadType == "left") {
						txtInput.x = end.x + WF.LINE_HEAD_W;
						txtInput.y = end.y - WF.LINE_HEAD_H - txtInput.height;
					} else if(this.lineHeadType == "right") {
						txtInput.x = end.x - WF.LINE_HEAD_W - WF.LINE_TXT_W;
						txtInput.y = end.y - WF.LINE_HEAD_H - txtInput.height;
					} else if(this.lineHeadType == "up") {
						txtInput.x = end.x - (WF.LINE_TXT_W / 2);
						txtInput.y = end.y + WF.LINE_HEAD_W;
					} else {
						txtInput.x = end.x - (WF.LINE_TXT_W / 2);
						txtInput.y = end.y - WF.LINE_TXT_H - WF.LINE_HEAD_W;
					}
					this.distanceWithEndPointWidth = this.txtInput.x - end.x;
					this.distanceWithEndPointHeight = this.txtInput.y - end.y;
				}
				txtInput.setFocus();
				this.txtLabel.width = 0;
				this.txtLabel.height = 0;
			}
		}
		
		//根据线的移动移动文本框
		public function movingTxtByLine():void {
			var end:Point = this.points.getItemAt(this.points.length - 1) as Point;
			this.txtLabel.x = end.x + this.distanceWithEndPointWidth;
			this.txtLabel.y = end.y + this.distanceWithEndPointHeight;
		}
		
		//初始化基本参数，并画线
		public function paint(fromX:Number, fromY:Number, x:Number, y:Number):Line {
			this.points.removeAll();
			var itemSize:Number = WF.ITEM_SIZE;
			startPoint = new Point(fromX + (itemSize / 2) + this.resizeFromExtraX, fromY + (itemSize / 2) + this.resizeFromExtraY);
			this.points.addItem(startPoint);
			initPoints(fromX, fromY, x, y);
			endPoint = new Point(x + (itemSize / 2) + this.resizeToExtraX, y + (itemSize / 2) + this.resizeToExtraY);
			this.points.addItem(endPoint);
			this.drawLine();
			return this;
		}
		
		/** 
		 *  @param type 线的类型 - 是sourceItem或toItem 的线
		 **/
		public function repaintLine(type:String="to"):void {}
		//由子类实现，根据 fromX、fromY、x、y 初始化页面的点
		protected function initPoints(fromX:Number, fromY:Number, x:Number, y:Number):void {}
		//移动中间点
		public function resizeMiddleItem():void {}
		//移动start点，让线尾不重叠的
		public function resizeFromItem():void {}
		//移动end点，让线头不重叠的
		public function resizeEndItem():void {}
		
		/**
		 * 画线：1、画出points点连成的线 
		 *             2、画出线头
		 **/
		public function drawLine():void {
			var lineColor:Number;
			if(this.isClick) {
				lineColor = WF.LINE_CLICK_COLOR;
			} else {
				lineColor = WF.LINE_COLOR;	
			}
			
			this.graphics.clear();
			this.graphics.lineStyle(WF.LINE_SIZE, lineColor);
			this.graphics.moveTo(startPoint.x, startPoint.y);
			for(var i:int=1; i<this.points.length; i++) {
				var point:Point = this.points.getItemAt(i) as Point;
				this.graphics.lineTo(point.x, point.y);
			}
			
			drawLineHead(lineColor);																																	//画出线头
			moveDirWithLining();																																			//重新设置resize 点的位置
		}
		
		/** 
		 * 【线-箭头】实际为3个点组成、根据3个点练成三角形 - 组成箭头
		 **/
		public function drawLineHead(lineColor:Number):void {
			this.graphics.lineStyle(0, lineColor);																												//设置线的颜色
			this.graphics.beginFill(lineColor, 5);																												//设置填充颜色
			this.graphics.moveTo(endPoint.x, endPoint.y);																							//三角形的开始点和结束点都是 endPoint.x 和  endPoint.y
			
			if(this.lineHeadType == "up") {
				this.graphics.lineTo(endPoint.x - WF.LINE_HEAD_H, endPoint.y + WF.LINE_HEAD_W);
				this.graphics.lineTo(endPoint.x + WF.LINE_HEAD_H, endPoint.y + WF.LINE_HEAD_W);
			}
			else if(this.lineHeadType == 'down') {
				this.graphics.lineTo(endPoint.x - WF.LINE_HEAD_H, endPoint.y - WF.LINE_HEAD_W);
				this.graphics.lineTo(endPoint.x + WF.LINE_HEAD_H, endPoint.y - WF.LINE_HEAD_W);
			}
			else if(this.lineHeadType == "left") {
				this.graphics.lineTo(endPoint.x + WF.LINE_HEAD_W, endPoint.y - WF.LINE_HEAD_H);
				this.graphics.lineTo(endPoint.x + WF.LINE_HEAD_W, endPoint.y + WF.LINE_HEAD_H);
			}
			else if(this.lineHeadType == "right") {
				this.graphics.lineTo(endPoint.x - WF.LINE_HEAD_W, endPoint.y - WF.LINE_HEAD_H);
				this.graphics.lineTo(endPoint.x - WF.LINE_HEAD_W, endPoint.y + WF.LINE_HEAD_H);
			}
			
			this.graphics.lineTo(endPoint.x, endPoint.y);																								//三角形的开始点和结束点都是 endPoint.x 和  endPoint.y
			this.graphics.endFill();
		}
		
		public function hidenInput():void {
			if(isInputEditing) {
				txtLabel.text = this.txtInput.text;
				WF.wp.transferVariablePanel.vc.txtDesc.text = this.txtLabel.text;
				this.variable.desc = this.txtLabel.text;
				
				WP.isMovingNodeContainer = true;
				this.isInputEditing = false;
				txtLabel.width = getLabelDescWidth();
				txtLabel.height = this.txtInput.height;
				txtLabel.x = this.txtInput.x;
				txtLabel.y = this.txtInput.y;
				this.txtInput.width = 0;
				this.txtInput.height = 0;
			}
		}
		
		public function deleteMyself():void {
			this.removeElement(this.txtInput);
			this.removeElement(this.txtLabel);
			this.graphics.clear();
			this.removeDir();
			this.sourceItem.sourceLines.removeItem(this);
			this.targetItem.targetLines.removeItem(this);
			this.sourceItem = null;
			this.targetItem = null;
		}
		
		public function addDir():void {
			if(!this.isAddDir) {
				isAddDir = true;
				this.addElement(resizeFromDir);
				this.addElement(resizeToDir);
				if(this.middleDir != null) {
					this.addElement(middleDir);
				}
			}
		}
		
		public function removeDir():void {
			if(this.isAddDir) {
				this.isAddDir = false;
				this.removeElement(this.resizeFromDir);
				this.removeElement(this.resizeToDir);
				if(this.middleDir != null) {
					this.removeElement(this.middleDir);
				}
			}
		}
		
		private function moveDirWithLining():void {																																				//拖拽框移动
			resizeFromDir.x = this.getFromDirX();
			resizeFromDir.y = this.getFromDirY();
			resizeToDir.x = this.getEndDirX();
			resizeToDir.y = this.getEndDirY();
			if(this.middleDir != null) {
				this.middleDir.x = this.getMiddleDirX();
				this.middleDir.y = this.getMiddleDirY();
			}
		}
		
		private function getMiddleDirX():Number {
			var p1:Point = this.points.getItemAt(1) as Point;
			var p2:Point = this.points.getItemAt(2) as Point;
			var temp:Number = p1.x;
			if(p1.x > p2.x) temp = p2.x;
			return Math.abs((p2.x - p1.x)/2) + temp - (WF.ITEM_SIZE / 2);
		}
		
		private function getMiddleDirY():Number {
			var p1:Point = this.points.getItemAt(1) as Point;
			var p2:Point = this.points.getItemAt(2) as Point;
			var temp:Number = p1.y;
			if(p1.y > p2.y) temp = p2.y;
			return Math.abs((p2.y - p1.y) / 2) + temp - (WF.ITEM_SIZE / 2);
		}
		
		private function getFromDirX():Number {
			var p1:Point = this.points.getItemAt(1) as Point;
			var temp:Number = this.startPoint.x;
			if(this.startPoint.x > p1.x) temp = p1.x;
			return Math.abs((p1.x - this.startPoint.x)/2) + temp - (WF.ITEM_SIZE / 2);
		}
		
		private function getFromDirY():Number {
			var p1:Point = this.points.getItemAt(1) as Point;
			var temp:Number = this.startPoint.y;
			if(this.startPoint.y > p1.y) temp = p1.y;
			return Math.abs((p1.y - this.startPoint.y) / 2) + temp - (WF.ITEM_SIZE / 2);
		}
		
		private function getEndDirX():Number {
			var p:Point = this.points.getItemAt(this.points.length - 2) as Point;
			var temp:Number = p.x;
			if(p.x > endPoint.x) temp = endPoint.x;
			var headExt:Number = 0;
			if(this.lineHeadType == "right" || this.lineHeadType == "left") {
				headExt = WF.LINE_HEAD_W;
			}
			return Math.abs((this.endPoint.x - p.x - headExt) / 2) + temp - WF.ITEM_SIZE / 2;
		}
		
		private function getEndDirY():Number {
			var p:Point = this.points.getItemAt(this.points.length - 2) as Point;
			var temp:Number = p.y;
			if(p.y > endPoint.y) temp = endPoint.y;
			var headExt:Number = 0;
			if(this.lineHeadType == "up" || this.lineHeadType == "down") {
				headExt = WF.LINE_HEAD_W;
			}
			return Math.abs((this.endPoint.y - p.y - headExt) / 2) + temp - (WF.ITEM_SIZE / 2);
		}
		
		private function getLabelDescWidth():Number {
			var num:Number = 0;
			if(this.variable != null) 
			{
				if(this.variable.desc != null) {
					num = this.variable.desc.length * 14 + 6;
				}
			}
			return num;
		}
		
		
	}
}